<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入用户名"
        v-model="search"
        style="width: 200px"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>
      <el-select
        v-model="type"
        placeholder="请选择"
        @change="findByType"
        style="width: 100px; margin-left: 20px"
        clearable
      >
        <el-option
          v-for="items in t1"
          :key="items.value"
          :label="items.name"
          :value="items.id"
        >
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 20px" @click="add"
        >添加</el-button
      >
    </div>
    <!--  表格-->
    <!-- height:76vh;overflow: scroll; -->
    <el-table :data="tableData" height="76vh" border stripe style="width: 99%;">
      <el-table-column prop="account" label="用户名"> </el-table-column>
      <!-- <el-table-column prop="password" label="密码"> </el-table-column> -->
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="type1" label="权限">
        
         </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            @click="handleEdit(scope.row)"
            size="mini"
            type="primary"
            plain
            >编辑</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            type="danger"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加用户信息"
      v-model="dialogVisible"
      width="30%"
      :before-close="dialogClose"
    >
      <el-form :model="form" label-width="120px" :rules="rules" ref="form">
        <el-form-item label="账号" style="width: 80%" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" style="width: 80%" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item label="姓名" style="width: 80%" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限" style="width: 80%" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogClose">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <!--        更新学生信息-->
    <el-dialog title="编辑信息" v-model="dialogVisible1" width="30%">
      <el-form :model="form" label-width="120px" ref="form" :rules="rules">
        <el-form-item label="账号" style="width: 80%" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码" style="width: 80%" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item> -->

        <el-form-item label="姓名" style="width: 80%" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限" style="width: 80%" prop="type">
          
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in t1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取 消</el-button>
          <el-button  @click="updatePwd" type="warning">重置密码</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "Authority",
  components: {},
  data() {
    return {
      rules:{
        account:[{required: true, message: "请输入账号", trigger: "blur"}],
        password:[{required: true, message: "请输入密码", trigger: "blur"}],
        name:[{required: true, message: "请输入姓名", trigger: "blur"}],
        type:[{required: true, message: "请选择用户类型", trigger: "blur"}]
      },
      //当前页
      currentPage: 1,
      pageSize: 10,
      total: 0,
      //弹窗默认关闭
      dialogVisible: false,
      dialogVisible1: false,
      search: "",
      form: {},
      tableData: [],
      options: [
        { id: 1, name: "管理员" },
        { id: 2, name: "老师" },
        { id: 3, name: "学生" },
        { id: 4, name: "班主任" },
      ],

      t1: [
        { id: 1, name: "管理员" },
        { id: 2, name: "老师" },
        { id: 3, name: "学生" },
        { id: 4, name: "班主任" },
      ],
      type: "",
    };
  },
  created() {
    this.load();
  },
  //定义函数
  methods: {
    // 查询方法
    load() {
      request
        .get("/user/userQuery", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            account: this.search,
            type: this.type,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          // console.log(res.data.records[2].classList[0].className)
          console.log(res.data.records);
          for (let i = 0; i <= res.data.records.length; i++) {
            if (res.data.records[i].type == 1) {
              this.tableData[i].type1 = "管理员";
            }
            if (res.data.records[i].type == 2) {
              this.tableData[i].type1 = "老师";
            }
            if (res.data.records[i].type == 3) {
              this.tableData[i].type1 = "学生";
            }
            if (res.data.records[i].type == 4) {
              this.tableData[i].type1 = "班主任";
            }
          }
        });
    },
    //新增学生
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          request.post("/user/add", this.form).then((res) => {
            console.log(this.form.classId);
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.dialogVisible = false;
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
            this.load();
          });
        }
      });
    },
    //更新学生信息
    update() {
      request.put("/user/update", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
        } else {
          this.$message({
            type: "error",
            message: "更新失败",
          });
        }
        this.load();
        this.dialogVisible1 = false;
      });
    },
    updatePwd(){
      console.log(this.form.id);
      request.put("/user/updatepwd?id="+this.form.id).then(res =>{
        console.log(res);
        if (res.code ==="0") {
          this.dialogVisible1 = false
          this.$message({
            type:"success",message:"重置成功！密码为123456"
          })
          this.load()
        }else{
            this.$message({
            type:"error",message:res.message
          })
        }
      })
    },
    //删除学生
    handleDelete(id) {
      console.log(id);
      this.$confirm("是否删除该学生?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/user/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    findByType() {
      this.load();
    },
    //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
    //打开dialogVisible弹窗
    add() {
      this.dialogVisible = true;
      //清空弹窗的内容
      this.form = {};
    },
    //打开编辑弹框
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible1 = true;
    },
    // 关闭弹窗事件
    dialogClose() {
      //清空表单验证信息
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    findClass() {
      request.get("/class/all", {}).then((res) => {
        console.log(res);
        this.options = res.data;
        console.log(this.options[0].className);
      });
    },
  },
};
</script>
<style scoped>

</style>
